package com.ch.web.mq;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class RabbitMqProxy implements MethodInterceptor {
    private Object targetObject;

    public RabbitMqProxy() {
    }


    /**
     * 代表目标类
     *
     * @param targetObject
     */
    public RabbitMqProxy(Object targetObject) {
        this.targetObject = targetObject;
    }


    public Object createProxyInstance() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.targetObject.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    /**
     * 代理的目的是构造参数
     *
     * @param obj
     * @param method
     * @param args
     * @param methodProxy
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        //做日志处理...
        return method.invoke(this.targetObject, args);
    }
}
